package com.dao.zk;

import com.bean.zk.Role;
import com.util.zk.Dbutil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

@Repository
public class RoleDAO {
    /**
     * 获取所有角色
     *
     * @return
     */

    public List<Map<String, Object>> getRoles() {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        List<Map<String, Object>> roles = null;
        try {
            con = Dbutil.getConnection();
            String sql = "select * from role";
            roles = query.query(con, sql, new MapListHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return roles;
    }

    /**
     * 根据eid添加角色（emp_role）
     */
    public int addRoleById(int eid, Object[] ids) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "insert into   emp_role values(null,?,?)";

            for (int i = 0; i < ids.length; i++) {
                Object[] args = new Object[]{eid, ids[i]};
                int update = query.update(con, sql, args);
                effect += update;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }

    /**
     * 根据eid删除角色（emp_role）
     */
    public int removeRoleById(int eid, Object[] ids) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "		delete from emp_role where eid = ? and rid = ?";
            for (int i = 0; i < ids.length; i++) {
                Object[] args = new Object[]{eid, ids[i]};
                int update = query.update(con, sql, args);
                effect += update;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }

    /**
     * 根据eid查询角色（emp_role）
     */
    public List<Map<String, Object>> geRolesByEid(int eid) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        List<Map<String, Object>> roles = null;
        try {
            con = Dbutil.getConnection();
            String sql = "select rid from  emp_role where eid = ?";
            roles = query.query(con, sql, new MapListHandler(), eid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return roles;
    }


    /**
     * 根据eid删除角色（role）
     */
    public int delRoleById(int rid) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "delete from role where rid = ?";
            effect = query.update(con, sql, rid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }
    /**
     * 根据eid修改角色（role）
     */
    public int updateRoleById(String rname,String remark,int rid) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "update  role set rname = ? ,remark = ? where rid = ?";
            Object[] args = new Object[]{rname,remark,rid};
             effect = query.update(con, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }
    /**
     * 添加角色（role）
     */
    public int addRole(String rname,String remark) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "insert into  role values(null,?,?)";
            Object[] args = new Object[]{rname,remark};
             effect = query.update(con, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }

    /**
     * 添加角色（role）
     */
    public int addRoleByRid(Role role) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        int effect = 0;
        try {
            con = Dbutil.getConnection();
            String sql = "insert into  role values(?,?,?)";
            Object[] args = new Object[]{role.getRid(),role.getRname(),role.getRemark()};
             effect = query.update(con, sql, args);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return effect;
    }


    public Role geRoleByRid(int rid) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        Role role = null;
        try {
            con = Dbutil.getConnection();
            String sql = "select * from  role where rid = ?";
            role = query.query(con, sql, new BeanHandler<>(Role.class), rid);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return role;
    }

    public Role geRoleByRname(String rname) {
        Connection con = null;
        QueryRunner query = new QueryRunner();
        Role role = null;
        try {
            con = Dbutil.getConnection();
            String sql = "select * from  role where rname = ?";
            role = query.query(con, sql, new BeanHandler<>(Role.class), rname);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            Dbutil.closeAll(null,null,con);
        }
        return role;
    }
}
